package zmee.pur.src.formplugin;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.IFormView;

import java.util.EventObject;

/**
 * @Author: xw
 * @Date: 2022/12/06/14:31
 * @Description: 投标单通用报价单分录金额字段设置默认值
 */
public class PurListPriceDefaultValueBillPlugin extends AbstractBillPlugIn {

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        IFormView parentView = this.getView().getParentView();
        if (parentView != null) {
            DynamicObject dataEntity = parentView.getModel().getDataEntity();
            String name = dataEntity.getDataEntityType().getName();
            if ("tnd_tenderbill".equals(name)) {
                String billStatus = dataEntity.getString("billstatus");
                if ("A".equals(billStatus)) {
                    boolean check = (boolean) this.getModel().getValue("zmee_check");
                    if (check) {
                        return;
                    }
                    this.getModel().setValue("zmee_check", true);
                    DynamicObjectCollection entryEntity = this.getModel().getDataEntity(true).getDynamicObjectCollection("entryentity");
                    for (int i = 0; i < entryEntity.size(); i++) {
                        this.getModel().setValue("amount", 1, i);
                        this.getModel().setValue("price", 1, i);
                        this.getModel().setValue("taxprice", 1, i);
                        this.getModel().setValue("taxamount", 1, i);
                        this.getModel().setValue("taxitem", 6L, i);
                    }
                    setEntryPropertiesVisible();
                }
            }
        }
    }

    private void setEntryPropertiesVisible() {
        String[] properties = {"amount", "price", "taxprice", "taxamount", "taxitem", "currency"};
        this.getView().setVisible(false, properties);
    }
}
